package com.meilai.project.controller.web.administration.goods;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.administration.goods.GoodsOutStorageDTO;
import com.meilai.project.dto.administration.goods.GoodsOutStorageSearchDTO;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.service.administration.goods.GoodsOutStorageService;
import com.meilai.project.vo.administration.goods.GoodsOutStorageVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "出库管理", tags = {"出库管理"})
@RestController
@RequestMapping("api/web/goods_out_storage_manage")
public class GoodsOutStorageController {

    @Autowired
    private GoodsOutStorageService service;

    @ApiOperation(value = "所有出库单列表", notes = "库管，查看所有出库记录", response = GoodsOutStorageVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("list")
    public CommonResult list(
            @ApiParam(name = "page", value = "当前页", required = true)
            @RequestParam(value = "page", defaultValue = "1", required = true) Integer page,
            @ApiParam(name = "page_size", value = "每页数量", required = true)
            @RequestParam(value = "page_size", defaultValue = "10", required = true) Integer page_size,
            @ApiParam(name = "goods_type_ids", value = "物品类型id", required = false)
            @RequestParam(value = "goods_type_id", required = false) Long goods_type_id,
            @ApiParam(name = "number_min", value = "数量最小值", required = false)
            @RequestParam(value = "number_min", required = false) Long number_min,
            @ApiParam(name = "number_max", value = "数量最大值", required = false)
            @RequestParam(value = "number_max", required = false) Long number_max,
            @ApiParam(name = "user_id", value = "用户id", required = false)
            @RequestParam(value = "user_id", required = false) Long user_id) {

        GoodsOutStorageSearchDTO searchDTO = new GoodsOutStorageSearchDTO();
        searchDTO.setGoods_type_id(goods_type_id);
        searchDTO.setNumber_min(number_min);
        searchDTO.setNumber_max(number_max);
        searchDTO.setUser_id(user_id);
        return new CommonResult().success(service.selectPageList(page, page_size, searchDTO));
    }


//    @ApiOperation(value = "本人出库单列表", notes = "自己的出库单列表记录", response = GoodsOutStorageVO.class)
//    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
//            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
//            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
//            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
//            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
//    @ResponseBody
//    @GetMapping("own_list")
//    public CommonResult Ownlist() {
//
//        return new CommonResult().success(service.selectOwnList());
//    }

    @ApiOperation(value = "详情", notes = "详情", response = GoodsOutStorageVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("detail/{id}")
    public CommonResult detail(@ApiParam(name = "id", value = "数据id", required = true) @PathVariable("id") Long id) {
        return new CommonResult().success(service.getOneById(id));
    }


    @ApiOperation(value = "保存", notes = "保存")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("save")
    public CommonResult save(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) GoodsOutStorageDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.save(dto)) {
                return new CommonResult().success("新增成功");
            } else {
                return new CommonResult().failed("新增失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

//    @ApiOperation(value = "提交", notes = "提交")
//    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
//            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
//            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
//            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
//            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
//    @ResponseBody
//    @PostMapping("begin")
//    public CommonResult begin(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) GoodsOutStorageDTO dto
//            , BindingResult bindingResult) {
//        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
//        try {
//            //提交需要记录提交时间
//            dto.setSubmit_at(LocalDateTime.now());
//            if (service.begin(dto)) {
//                return new CommonResult().success("新增成功");
//            } else {
//                return new CommonResult().failed("新增失败");
//            }
//        } catch (Exception e) {
//            return new CommonResult().failed(e.getMessage());
//        }
//    }
//
//    @ApiOperation(value = "通过", notes = "通过")
//    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
//            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
//            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
//            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
//            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
//    @ResponseBody
//    @PutMapping("pass")
//    public CommonResult pass(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) GoodsOutStorageCheckDTO dto
//            , BindingResult bindingResult) {
//        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
//        try {
//            if (service.pass(dto)) {
//                return new CommonResult().success("通过成功");
//            } else {
//                return new CommonResult().failed("通过失败");
//            }
//        } catch (Exception e) {
//            return new CommonResult().failed(e.getMessage());
//        }
//    }
//
//    @ApiOperation(value = "驳回", notes = "驳回")
//    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
//            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
//            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
//            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
//            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
//    @ResponseBody
//    @PutMapping("return_back")
//    public CommonResult returnBack(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) GoodsOutStorageCheckDTO dto
//            , BindingResult bindingResult) {
//        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
//        try {
//            if (service.returnBack(dto)) {
//                return new CommonResult().success("驳回成功");
//            } else {
//                return new CommonResult().failed("驳回失败");
//            }
//        } catch (Exception e) {
//            return new CommonResult().failed(e.getMessage());
//        }
//    }
//
//    @ApiOperation(value = "删除", notes = "删除")
//    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
//            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
//            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
//            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
//            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
//    @ResponseBody
//    @DeleteMapping("delete")
//    public CommonResult delete(@RequestParam(value = "id", required = true) Long id) {
//        try {
//            if (service.delete(id)) {
//                return new CommonResult().success("删除成功");
//            } else {
//                return new CommonResult().failed("删除失败");
//            }
//        } catch (Exception e) {
//            return new CommonResult().failed(e.getMessage());
//        }
//    }
//
//    @ApiOperation(value = "彻底删除", notes = "彻底删除")
//    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
//            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
//            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
//            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
//            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
//    @ResponseBody
//    @DeleteMapping("actual_delete")
//    public CommonResult actualDelete(@RequestParam(value = "id", required = true) Long id) {
//        try {
//            if (service.actualDelete(id)) {
//                return new CommonResult().success("彻底删除成功");
//            } else {
//                return new CommonResult().failed("彻底删除失败");
//            }
//        } catch (Exception e) {
//            return new CommonResult().failed(e.getMessage());
//        }
//    }


    @GetMapping("/getStorageNumber")
    @ApiOperation("获取物品库存")
    public ApiResult<Long> getStorageNumber(@RequestParam("id") Long id) {
        return ApiResult.ok(service.getStorageNumber(id));
    }
}
